package fr.aerian.imageconverter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.ByteBuffer;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		main();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	public Bitmap convertBitmap(Bitmap bm){
		int bm_width = bm.getWidth(), bm_height = bm.getHeight();

		int R, G, B,A;

		for (int y = 0; y < bm_height; y++){
			for (int x = 0; x < bm_width; x++) {
				int pixel = bm.getPixel(x, y);
				A = (pixel >> 24) & 0xFF;
				R = (pixel >> 16) & 0xFF;
				G = (pixel >> 8) & 0xFF;
				B = pixel & 0xFF;
				bm.setPixel(x, y, Color.argb(A,B,G,R));
				//R,G.B - Red, Green, Blue
				//to restore the values after RGB modification, use 
				//next statement
			}}
		return bm;
	}
	
	public void WriteSettings(Context context, String data){ 
		 File file = new File(Environment.getExternalStorageDirectory(), "result.txt");
		  
		 try {
			file.createNewFile();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		 FileWriter filewriter;
		try {
			filewriter = new FileWriter(file,true);
			filewriter.write(data);
			filewriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void main(){
		//Init
		long startTime, duration,initTime;
		initTime = startTime = System.currentTimeMillis();
		File sdDir = Environment.getExternalStorageDirectory();
		TextView tv = (TextView) findViewById(R.id.textViewDuration);

		String strFileName = sdDir.toString() + "/LinuxAdeneoBGR.jpg";
		StringBuilder content = new StringBuilder("External Directory: "+strFileName.toString()+"\n");

		Bitmap srcBmp = BitmapFactory.decodeFile(strFileName);
		if (srcBmp == null){
			Log.i("FAILED:", "Could not find " + strFileName);
			try {
				throw new Exception();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		int bytes = srcBmp.getWidth()*srcBmp.getHeight()*4;
		ByteBuffer src_buffer = ByteBuffer.allocate(bytes); //Create a new buffer
		ByteBuffer dest_buffer = ByteBuffer.allocate(bytes); //Create a new buffer
		srcBmp.copyPixelsToBuffer(src_buffer); //Move the byte data to the buffer
		srcBmp.copyPixelsToBuffer(dest_buffer);
		byte[] src_array = src_buffer.array();
		byte[] dest_array = dest_buffer.array();
		
		//Tab
		long durationTab[] = new long[10];
		int i;
		
		NativePictureConverter nativePictureConverter = new NativePictureConverter();
		
		duration = System.currentTimeMillis() - startTime;

		WriteSettings(this, "Init Part\n"+String.valueOf(duration)+" ms \n");
		//Log init
		content.append(getResources().getString(R.string.time_loading) +Long.toString(duration) + " ms\n");
		//Display init
		ImageView iv = (ImageView) findViewById(R.id.src_img);
		iv.setImageBitmap(srcBmp);

		//Java
		Bitmap dstBmp = srcBmp.copy(Config.ARGB_8888, true);
		duration = 0;
		WriteSettings(this, "Java Part\n");
		for(i = 0; i< 10; i++){
			startTime = System.currentTimeMillis();
			dstBmp = convertBitmap(dstBmp);
			durationTab[i] = System.currentTimeMillis() - startTime;
			duration += durationTab[i];
			WriteSettings(this, String.valueOf(durationTab[i])+" ms \n");
		}

		//Log Java
		content.append(getResources().getString(R.string.time_converting) +Long.toString(duration/durationTab.length) + " ms\n");

		//C
		duration = 0;
		WriteSettings(this, "C Part\n");
		for(i = 0; i< 10; i++){
			startTime = System.currentTimeMillis();
			nativePictureConverter.ConvertBGRtoRGB(src_array, dest_array, srcBmp.getWidth(), srcBmp.getHeight(),1);
			dstBmp.copyPixelsFromBuffer(ByteBuffer.wrap(dest_array));
			durationTab[i] = System.currentTimeMillis() - startTime;
			duration += durationTab[i];
			WriteSettings(this, String.valueOf(durationTab[i])+" ms \n");
		}

		//Log C
		content.append(getResources().getString(R.string.time_converting_c) +Long.toString(duration/durationTab.length) + " ms\n");
		
		//Neon
		duration = 0;
		WriteSettings(this, "Neon Part\n");
		for(i = 0; i< 10; i++){
			startTime = System.currentTimeMillis();
			nativePictureConverter.ConvertBGRtoRGB(src_array, dest_array, srcBmp.getWidth(), srcBmp.getHeight(),0);
			dstBmp.copyPixelsFromBuffer(ByteBuffer.wrap(dest_array));
			durationTab[i] = System.currentTimeMillis() - startTime;
			duration += durationTab[i];
			WriteSettings(this, String.valueOf(durationTab[i])+" ms \n");
		}
		
		//Log Neon
		content.append(getResources().getString(R.string.time_converting_neon) +Long.toString(duration/durationTab.length) + " ms\n");
		
		//Total Log
//		content.append(getResources().getString(R.string.time) +Long.toString(duration+startTime-initTime) + " ms\n");
		
		//Setting Display & Info
		Log.i("INFO:", content.toString());
		tv.setText(content.toString());
		ImageView iv2 = (ImageView) findViewById(R.id.dest_img);
		iv2.setImageBitmap(dstBmp);
	}
}
